#include "MainHeader.h"

int             i;
int             sMessage(char *messagestring)
{
	MessageBox(Main, messagestring, NULL, MB_OK);
	return 0;
}
int             HandleDetections(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	FILE           *fp = NULL;
	
	int             sprch = 0;
	char            cBuf[1024] = {0};
	int             i = 0;
	int             loadit = 0;
	long            LevelCounter = 0;
	u32             BIC = 0;
	HWND            debug = NULL;
	nRoomBuff*      RoomBuff;
	switch (LOWORD(wParam))
	{
	case cmdLPE:
		// if(GameEngine->isZM()==-1) return  0;
		ShowWindow(hwndLPE, 1);

		break;

	case mnuEB:
		if (GameEngine->NotOpen())
		return 0;
		if(GBA.ReturnFileName("Please select a bitmap file\0*.bmp\0", cBuf, 512,0)){
			
			fp = fopen(cBuf, "w+");
			if (fp)
			{
				// BitBlt(img[1].DC(),
				// 0,0,ThisLevelData->X*16,ThisLevelData->Y*16,ThisBackBuffer.DC(),ThisLevelData->X*16,ThisLevelData->Y*16,SRCCOPY);

				ThisBackBuffer.SaveToFile(fp);
				fclose(fp);
			} else
			{
				MessageBox(0, "Bad File Name", "Error", MB_OK);
			}
		}
		break;
	case mnuInsert:
		ShowWindow(BGi.me,1);
		break;
	case chkHS:
		DrawRoom();
		break;
	case mnuIR:
		if (GameEngine->NotOpen())
		return 0;
		GBA.ReturnFileName("Please Select a file to export too\0*.DHR", cBuf, 1024,0);
		//Import(cBuf);
		break;
	case mnuIPSP:
		if (GameEngine->NotOpen())
		return 0;
		GBA.ReturnFileName("Please Select an Apply File to apply\0*.ips", cBuf, 1024);
		ApplyIPS(cBuf, GBA.FileLoc);
		break;
	case mnuER:
		if (GameEngine->NotOpen())
		return 0;
		GBA.ReturnFileName("Please Select a file to export too\0*.DHR", cBuf, 1024,0);
		// Export(cBuf);
		break;

	case chkDTrans:
		if (GameEngine->NotOpen())
		return 0;
		ThisForeground->Dirty = 1;
		DrawRoom();
		break;
	case cboClip:
		if (HIWORD(wParam) == CBN_SELCHANGE)
		{
			if (ViewClip.value() == 1)
			{
				// ViewClip.value(0);     
				// DrawClipIdent();
				DrawRoom();

			}
		}
		break;
	case mnuClipboard:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(ClipBoard.me, SW_SHOW);
		break;

	case mnuSOE:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(OamEditor.me, SW_SHOW);
		break;
	case cmdES:

		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(SceneWnd, SW_SHOW);
		ShowWindow(TSScene, SW_SHOW);
		break;
	case mnuSSE:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(SpriteSet.me, SW_SHOW);
		break;
	case mnuTSA:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(MyTSAEditor.tMain, SW_SHOW);
		break;
	case mnuHeader:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(hwndHeader, SW_SHOW);
		break;
	case mnuRS:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(ExtendedOptWND, SW_SHOW);
		LoadExtensions();
		LayerCombo.SetListIndex(1);
		SendMessage(ExtendedOptWND, WM_COMMAND, 0x000103eb, 0);

		break;
	case mnuTE:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(TextEditor.tMain, SW_SHOW);
		break;
	case mnuSE:
		if (GameEngine->NotOpen())
		return 0;
		ShowWindow(StatEditor.me, SW_SHOW);
		break;
	case mnuOpen:
		if (GBA.ROM)
		fclose(GBA.ROM);

		GBA.ReturnFileName("Open a GBA ROM\0*.gba", GBA.FileLoc, 512);
		if (GBA.FileLoc[0])
		{
			GBA.REDIT = fopen(GBA.FileLoc, "r+b");
			GameEngine->OpenFile(GBA.FileLoc);
			char            ROMNAME[0x5] = {'\0'};
			char            MZMFILE[0x5] = {'\0'};
			char            MFFILE[0x5] = {'\0'};

			strcpy(MZMFILE, "BMXE");
			strcpy(MFFILE, "AMTE");
			fseek(GBA.REDIT, 0xAC, SEEK_SET);
			fread(ROMNAME, 1, 0x4, GBA.REDIT);
			char            c = 0;

			if ((ROMNAME[0] == MZMFILE[0]) && (ROMNAME[1] == MZMFILE[1]) && (ROMNAME[2] == MZMFILE[2]) && (ROMNAME[3] == MZMFILE[3]) &&
					(ROMNAME[4] == MZMFILE[4]))
			{ // everthing went ok!

				GameEngine->OpenFile(GBA.FileLoc);
				//GameEngine->isZM() = 0;
				GameEngine->Type(GameType::wZM,GameEngine);
				
				for( int i = 0;i < 4;i++) 	 
				{
					SetUpCombos(i);
				}
				PopulateSpriteArray();
				InitPosArray();
				EnableConnections();
				Combos[cClip].Enable();
				Combos[cSpriteSet].Enable();
				Combos[cRoom].Enable();
				Combos[cArea].Enable();
				if (DefArea > 7 || DefArea < 0)
				Combos[cArea].SetListIndex(0);
				else
				Combos[cArea].SetListIndex(DefArea);
				Minimap.Create();
				Minimap.cboMArea.SetListIndex(0);
				Minimap.DecompressMap();
				Minimap.DrawMap();
				Minimap.MFMap.Disable();
				StatEditor.Switch();
				TextEditor.Create();
				fseek(GameEngine->oFP(), 0x7c0430, SEEK_SET);
				fread(&BIC, 1, 4, GameEngine->oFP());
				if (BIC == 0x78004817)
				{
					BGiInstall = 1;
				}
				SendMessage(Main, WM_COMMAND, 0x000103ee, 0);

			} else if ((ROMNAME[0] == MFFILE[0]) && (ROMNAME[1] == MFFILE[1]) && (ROMNAME[2] == MFFILE[2]) && (ROMNAME[3] == MFFILE[3]) &&
					(ROMNAME[4] == MFFILE[4]))
			{
				GameEngine->OpenFile(GBA.FileLoc);
				//GameEngine->isZM() = 0;
				GameEngine->Type(GameType::wMF,GameEngine);
				EnableMenuItem(GetMenu(hwnd), 0, MF_BYPOSITION | MF_GRAYED);
				for( int i = 0;i < 4;i++) 	 
				{
					SetUpCombos(i);
				}
				PopulateSpriteArray();
				

				
				EnableConnections();
				Combos[cClip].Enable();
				Combos[cSpriteSet].Enable();
				Combos[cRoom].Enable();
				Combos[cArea].Enable();
				if (DefArea > 10 || DefArea < 0)
				Combos[cArea].SetListIndex(0);
				else
				Combos[cArea].SetListIndex(DefArea);
				Combos[cArea].SetListIndex(0);
				Minimap.Create();
				Minimap.cboMArea.SetListIndex(0);
				Minimap.DecompressMap();
				Minimap.DrawMap();
				Minimap.MFMap.Enable();
				TextEditor.Create();
				StatEditor.Switch();
				fseek(GBA.ROM, 0x7c0430, SEEK_SET);
				fread(&BIC, 1, 4, GBA.ROM);
				if (BIC == 0x78004819)
				{
					BGiInstall = 1;
				}
				SendMessage(Main, WM_COMMAND, 0x000103ee, 0);

			} else
			{
				sMessage("This is not the correct file.");
				fclose(GBA.REDIT);
				return 1;
				GameEngine->isZM() == -1;
			}
			if (GBA.REDIT)
			fclose(GBA.REDIT);
		}
		break;
	case mnuMini:
		if (GameEngine->NotOpen())
		return 0;

		ShowWindow(hwndMM, SW_SHOW);
		break;
	case cboSpriteSet:
		if (HIWORD(wParam) == CBN_SELCHANGE)
		{
			RHeader* RoomHeader=GameEngine->Members.RoomHeader;
			switch (Combos[cSpriteSet].GetListIndex())
			{
			case 0:
				sprch = RoomHeader->bSpriteIndex1;
				if (RoomHeader->bSpriteIndex1)
				loadit = 1;
				break;
			case 1:
				sprch = RoomHeader->bSpriteIndex2;
				if (RoomHeader->bSpriteIndex2)
				loadit = 1;
				break;
			case 2:
				sprch = RoomHeader->bSpriteIndex3;
				if (RoomHeader->bSpriteIndex3)
				loadit = 1;
				break;

			}
			if (loadit)
			{

				if (GameEngine->isZM())
				LoadSet(gfxpnt_dst, paltransfer, sprite_in, sprch);
				if (GameEngine->isMF())
				{

					MFLoadSet(gfxpnt_dst, paltransfer, sprite_in, sprch);
				}
				LoadPal(paltransfer, sprite_in, SprPal);
				LoadSpriteToMem(gfxpnt_dst, sprite_in, VRAM.RAM, &SpriteImage);
				DecodeOAM();

				for (i = 0; i < maxsprite; i++)
				{
					DrawOAM(i);
				}
				DrawRoom();

				return 0;
			}
		}
		break;
	case chkScroll:
		EnableWindow(cboScroll.thishwnd(),ScrollCheck.value());
		DrawRoom();
		break;
	case cmdSave:
		BIC=(GameEngine->isMF() ?
		(RoomOffsets[Combos[cArea].GetListIndex()] - 0x8000000)   + (Combos[cRoom].GetListIndex() * 0x3C) 
		: 
		(RoomOffsets[Combos[cArea].GetListIndex()] - 0x8000000) + (Combos[cRoom].GetListIndex() * 0x3C) 
		);
		if(!BIC||(BIC>0x7FFFFF)){
			MessageBox(hwnd,"Uh so something fucked up.","Boomb",MB_OK);
		}else{


			SaveLevel(GBA.ROM,BIC );
			
		}
		break;
	case chkForeground:
		// Check if it's enabled or disabled.
		CheckBoxes[Level].value(0);
		CheckBoxes[Background].value(0);
		CheckBoxes[Clip].value(0);
		break;
	case chkSView:
		if (GBA.ROM)
		// DrawScrolls(cboScroll.GetListIndex());
		DrawRoom();
		break;
	case cboDScroll:
		if(HIWORD(wParam)==CBN_SELCHANGE) DrawRoom();
		break;
	case mnuRR:
		ShowWindow(hwndResize, SW_SHOW);
		break;

	case chkLevel:
		// Check if it's enabled or disabled.
		CheckBoxes[Foreground].value(0);
		CheckBoxes[Background].value(0);
		CheckBoxes[Clip].value(0);
		break;
	case chkST:
		ShowWindow(hwndTileset, CheckBoxes[showtileset].value() * 5);
		break;
	case chkSM:
		ShowWindow(hwndMap, CheckBoxes[showmap].value() * 5);
		break;
	case chkBacklayer:
		// Check if it's enabled or disabled.
		CheckBoxes[Level].value(0);
		CheckBoxes[Foreground].value(0);
		CheckBoxes[Clip].value(0);
		break;
	case chkClip:
		// Check if it's enabled or disabled.
		CheckBoxes[Level].value(0);
		CheckBoxes[Foreground].value(0);
		CheckBoxes[Background].value(0);
		break;
	case chkViewF:
		if (!LoadingLevel)
		DrawRoom();
		break;
	case chkViewL:
		if (!LoadingLevel)
		DrawRoom();
		break;
	case chkViewB:
		if (!LoadingLevel)
		DrawRoom();
		break;
	case chkViewBG:
		if (!LoadingLevel)
		DrawRoom();
		break;
	case chkSRe:
		CheckBoxes[sMove].value(0);
		break;
	case chkSMove:
		CheckBoxes[sResize].value(0);
		break;
	case IDCANCEL:
		EndDialog(hwnd, 0);
		EndDialog(DoorWin, 0);
		EndDialog(hwndResize, 0);
		break;
	case chkVC:

		DrawRoom();

		break;
	case cboArea:
		if (HIWORD(wParam) == CBN_SELCHANGE)
		{
			GameEngine->OpenFile();
			
			if (GameEngine->isZM())
			{
				// if( Combos[cArea].GetListIndex() == 0){
				// LevelCounter = 0x29;

				// }else if( Combos[cArea].GetListIndex() == 1) {
				// LevelCounter = 0x2A;
				// }else 
				if (Combos[cArea].GetListIndex() == 6)
				{
					LevelCounter = 0x70;
				} else
				{

					LevelCounter = (RoomOffsets[Combos[cArea].GetListIndex() + 1] - RoomOffsets[Combos[cArea].GetListIndex()]) / 0x3C;
					;
				}
				// Execute Code 

			} else if (GameEngine->isMF())
			{

				if (Combos[cArea].GetListIndex() == 9)
				{
					LevelCounter = 3;
				} else if (Combos[cArea].GetListIndex() == 4)
				{
					LevelCounter = 0x34;
				} else
				{

					LevelCounter = (RoomOffsets[Combos[cArea].GetListIndex() + 1] - RoomOffsets[Combos[cArea].GetListIndex()]) / 0x3C;
				}

			}
			Combos[cRoom].Clear();

			for (i = 0; i < LevelCounter; i++)
			{
				sprintf(cBuf, "%x", i);
				Combos[cRoom].Additem(cBuf);

			}

			SetupDoors();

			if (DefRoom >= LevelCounter)
			Combos[cRoom].SetListIndex(LevelCounter);
			else
			Combos[cRoom].SetListIndex(DefRoom);

			PostMessage(hwnd, WM_COMMAND, 0x000103ef, 0); // Simulate room combo 
			// clicking.

			break;
		case cboRoom:
			if (HIWORD(wParam) == CBN_SELCHANGE)
			{  
				RHeader* RoomHeader=GameEngine->Members.RoomHeader;
				DrawAll=1;
			GameEngine->OpenFile();
				// HWND tmp = hwndMap;
				ThisForeground->Dirty = 1;
				ThisLevelData->Dirty = 1;
				ThisBackLayer->Dirty = 1;
				LoadingLevel = 1;
				ShowWindow(ExtendedOptWND, SW_HIDE);
				for(i=0;i<16;i++) {
					OverallSize[i].top=OverallSize[i].bottom=OverallSize[i].left=OverallSize[i].right=0;

				}
				if (GameEngine->isZM())
				{
					LoadHeader((RoomOffsets[Combos[cArea].GetListIndex()] - 0x8000000) + Combos[cRoom].GetListIndex() * 0x3C);
				} else if (GameEngine->isMF())
				{
					LoadHeader((RoomOffsets[Combos[cArea].GetListIndex()] - 0x8000000) + Combos[cRoom].GetListIndex() * 0x3C);
				}
				GetTileset(RoomHeader->bTileset);
				
				LoadData();
				LoadDoors();
				
				LoadEnemies();
				
				initScroll();
				GetScroll();
				cboScroll.Clear();
				for(i = 0; i<=scroll.Number+1;i++){
					sprintf(cBuf,"%x",i);
					cboScroll.Additem(cBuf);

				}
				cboScroll.SetListIndex(0);
				DrawLevel();
				
				RoomBuff=ThisForeground;
				RoomBuff->UndoBuff.UndoNum =0;

				RoomBuff=ThisLevelData;
				RoomBuff->UndoBuff.UndoNum =0;
				RoomBuff->UndoBuff.Set((RoomBuff->X*RoomBuff->Y)*2,(u16*)RoomBuff->TileBuf2D);

				RoomBuff=ThisBackLayer;
				RoomBuff->UndoBuff.UndoNum =0;
				RoomBuff->UndoBuff.Set((RoomBuff->X*RoomBuff->Y)*2,(u16*)RoomBuff->TileBuf2D);

				RoomBuff=ThisClipData;
				RoomBuff->UndoBuff.UndoNum =0;
				RoomBuff->UndoBuff.Set((RoomBuff->X*RoomBuff->Y)*2,(u16*)RoomBuff->TileBuf2D);
				//      DoesaBridgeExist();
				// UserEnableConnections();
				MyTSAEditor.DrawThisTileset();
				MyTSAEditor.LoadTSA();
				SendMessage(hwnd, WM_COMMAND, 0x00010408, 0); // Update cboSprite 
				// with SelChange

				LoadHeaderControls();
				for (i = 0; i < 16; i++)
				ClipBoard.Erase(i, 0xFFFF);
				LoadingLevel = 0;
				InvalidateRect(hwndTileset, 0, 1);
				SendMessage(hwndMap, WM_SIZE, 0, 1); // If lparam is 0 nothing
				// will happen
				// This will force an update

				RECT            t;

				GetWindowRect(hwndMap, &t);
				MoveWindow(hwndMap, t.left, t.top, 512, 496, 1);
				InvalidateRect(hwndMap, 0, 1);
				InvalidateRect(Minimap.hwndMiniMap, 0, 1);
			}
		}
		break;
	default:
		return 0;
	}

	return 0;
}

BOOL CALLBACK   DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	char            FilePath[1024] =
	{
		0
	};
	u16             wndrs[3] =
	{
		hsbC1, hsbC2, hsbC3
	};
	switch (message)
	{

	case WM_INITDIALOG:
		ClearGlobals();
		ThisLevelData=new nRoomBuff; 
		ThisBackLayer=new nRoomBuff; 
		ThisForeground=new nRoomBuff; 
		ThisClipData=new nRoomBuff; 
		ThisBackground=new nRoomBuff; 
		ThisRoomBuff=new nRoomBuff;
		InitPosArray();

		GetCurrentDirectory(1023, AppPath);
		strcpy(FilePath, AppPath);
		strcat(FilePath, "\\DH.cfg");
		LoadConfigFile(FilePath);
		DM[0] = DM[1] = 0;
		Main = hwnd;
		CurObjT = 0;
		CurObjNo = 0;

		InitTileset();
		InitMap();

		clrIndex = 0;

		img[0].Create(512, 1024);
		img[1].Create(1024, 1024);
		CheckBoxes[Foreground].SetCnt(GetDlgItem(Main, chkForeground));
		CheckBoxes[Level].SetCnt(GetDlgItem(Main, chkLevel));
		CheckBoxes[Level].value(2);
		ViewClip.SetCnt(GetDlgItem(Main, chkVC));
		CheckBoxes[Background].SetCnt(GetDlgItem(Main, chkBacklayer));
		ScrollCheck.SetCnt(GetDlgItem(Main, chkScroll));
		CheckBoxes[Clip].SetCnt(GetDlgItem(Main, chkClip));
		CheckBoxes[ViewF].SetCnt(GetDlgItem(Main, chkViewF));
		CheckBoxes[ViewL].SetCnt(GetDlgItem(Main, chkViewL));
		CheckBoxes[ViewB].SetCnt(GetDlgItem(Main, chkViewB));
		CheckBoxes[chkED].SetCnt(GetDlgItem(Main, chkViewBG));
		CheckBoxes[sMove].SetCnt(GetDlgItem(Main, chkSMove));
		CheckBoxes[sResize].SetCnt(GetDlgItem(Main, chkSRe));
		CheckBoxes[sView].SetCnt(GetDlgItem(Main, chkSView));
		CheckBoxes[showmap].SetCnt(GetDlgItem(Main, chkSM));
		CheckBoxes[showtileset].SetCnt(GetDlgItem(Main, chkST));
		CheckBoxes[chkES].SetCnt(GetDlgItem(Main, chkSprites));
		CheckBoxes[chkED].SetCnt(GetDlgItem(Main, chkDoors));
		
		// CheckBoxes[chkESC].SetCnt(GetDlgItem(Main,chkSprites));
		chkMC[0].SetCnt(GetDlgItem(Main, chkForeground2));
		chkMC[1].SetCnt(GetDlgItem(Main, chkLevel2));
		chkMC[2].SetCnt(GetDlgItem(Main, chkBacklayer2));
		chkMC[3].SetCnt(GetDlgItem(Main, chkClip2));
		chkDoTrans.SetCnt(GetDlgItem(Main, chkDTrans));
		chkDoTrans.value(1);
		CheckBoxes[showmap].value(1);
		CheckBoxes[showtileset].value(1);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmDoor), 0, DwProc);
		ShowWindow(DoorWin, SW_HIDE);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmTE), 0, TextEditorProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmSceneryEditor), 0, SceneProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmRoomStuff), 0, ExtendedProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmMiniMap), 0, MiniProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmHeader), 0, HeaderProced);
		CreateDialog(hGlobal, MAKEINTRESOURCE(fraTBE), 0, TSAProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(fraSS), 0, SSProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(fraStat), 0, SEProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(fraSSE), 0, SSEProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmOAM), 0, OAMProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmClipboard), 0, ClipBoardProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(fraBGI), 0, BGiProc);
		CreateDialog(hGlobal, MAKEINTRESOURCE(frmLPE), 0, LPProc);
		
		nMaxHScroll[sTileset] = (256 - nDisplayWidth[sTileset] + 7) / 8; // maximum 
		// H
		// scroll

		nMaxVScroll[sTileset] = (256 - nDisplayHeight[sTileset] + 7) / 8;

		ChangeScrollbars(hwnd, vsbScroll, sTileset);
		ChangeScrollbars(hwnd, vsbMap, sVMap);
		ChangeScrollbars(hwnd, hsbMap, sHMap);

		for (i = 0; i < 3; i++)
		{
			scrColors[i].create(GetDlgItem(hwndLPE, wndrs[i]), 0, 255);
			scrColors[i].ChangeScrollbars();
			// SetScrollPos( hwndTileset, SB_HORZ, 0, 1 );

		}

		Combos[1].Init(GetDlgItem(hwnd, (int) MAKEINTRESOURCE(IDC_COMBO1))); // GetDlgItem(hwnd,(int)MAKEINTRESOURCE(res))
		cboScroll.Init(GetDlgItem(hwnd,cboDScroll));
		Combos[cArea].Init(GetDlgItem(hwnd, (int) MAKEINTRESOURCE(cboArea)));
		Combos[cRoom].Init(GetDlgItem(hwnd, (int) MAKEINTRESOURCE(cboRoom)));
		Combos[cClip].Init(GetDlgItem(hwnd, (int) MAKEINTRESOURCE(cboClip)));
		Combos[cSpriteSet].Init(GetDlgItem(hwnd, (int) MAKEINTRESOURCE(cboSpriteSet)));
		Combos[cClip].Additem("Clipdata");
		Combos[cClip].SetListIndex(0);
		Combos[cSpriteSet].Additem("0");
		Combos[cSpriteSet].SetListIndex(0);
		Combos[cRoom].Additem("Rooms");
		Combos[cRoom].SetListIndex(0);

		Combos[cArea].Additem("Areas");
		Combos[cArea].SetListIndex(0);

		// etArrays();
		MPToUse = 0;
		mpTileset.Width = 1;
		mpTileset.Height = 1;
		mpTileset.sX = mpTileset.eX = mpTileset.cX = 0;
		mpTileset.sY = mpTileset.eY = mpTileset.cY = 0;
		mpMap.Width = 1;
		mpMap.Height = 1;
		mpMap.sX = mpMap.eX = mpMap.cX = 0;
		mpMap.sY = mpMap.eY = mpMap.cY = 0;


		
		//LoadTrans("[MainMenu]", 0, hwnd);
		break;
	case BN_CLICKED:

		break;
	case WM_COMMAND:

		HandleDetections(hwnd, message, wParam, lParam);
		break;

	case WM_VSCROLL: // exact same idea, but V scroll instead of H scroll

		// if((HWND)lParam == GetDlgItem(hwnd,vsbMap))
		// UpdateScroll(hwnd,wParam,1,vsbMap,sVMap);

		break;
	case WM_HSCROLL:

		// if((HWND)lParam == GetDlgItem(hwnd,hsbMap))
		// UpdateScroll(hwnd,wParam,NULL,hsbMap,sHMap);

		break;
	case WM_LBUTTONDOWN:
		// if((HWND)wParam == GetDlgItem(hwnd,picButton)) sMessage("Mouse was
		// clicked down");

		break;

	case WM_DESTROY:
		bRunApp = 0;
		if (GBA.ROM)
		fclose(GBA.ROM);
		// / myBackBuffer.Destroy();
		break;
	}

	return 0;
}

int WINAPI      WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	//GameEngine->isZM() = -1;
	hGlobal = hInstance;
	bRunApp = 1;
	DefRoom = 0;
	DefArea = 0;
	DefAddress = 0;
	Tile = 0;
	keyno=0;
	HICON           hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON7));
	MP[0] = MP[1] = 0;
	GameEngine=new Engine;
	DialogBox(hInstance, MAKEINTRESOURCE(frmMain), 0, DialogProc);
	delete ThisLevelData; 
	delete ThisBackLayer;
	
	delete ThisForeground;
	delete ThisClipData;
	delete ThisBackground;
	
	return 0;

}

int             SetUpCombos(int combon)
{
	char            quick[1023] =
	{
		0
	};
	char            cs[128] =
	{
		0
	};

	Combos[cSpriteSet].Clear();
	Combos[cSpriteSet].Additem("0");
	Combos[cSpriteSet].Additem("1");
	Combos[cSpriteSet].Additem("2");
	Combos[cSpriteSet].SetListIndex(0);
	if (combon == cArea)
	{
		Combos[combon].Clear();
		if (GameEngine->isZM())
		{
			Combos[cArea].Clear();
			Combos[cA1].Clear();
			Combos[cA2].Clear();
			Combos[cdA1].Clear();
			Combos[cdA2].Clear();

			Combos[cArea].Additem("Brinstar");
			Combos[cA1].Additem("Brinstar");
			Combos[cA2].Additem("Brinstar");
			Combos[cdA1].Additem("Brinstar");
			Combos[cdA2].Additem("Brinstar");
			Combos[cArea].Additem("Kraid");
			Combos[cA1].Additem("Kraid");
			Combos[cA2].Additem("Kraid");
			Combos[cdA1].Additem("Kraid");
			Combos[cdA2].Additem("Kraid");
			Combos[cArea].Additem("Norfair");
			Combos[cA1].Additem("Norfair");
			Combos[cA2].Additem("Norfair");
			Combos[cdA1].Additem("Norfair");
			Combos[cdA2].Additem("Norfair");
			Combos[cArea].Additem("Ridley");
			Combos[cA1].Additem("Ridley");
			Combos[cA2].Additem("Ridley");
			Combos[cdA1].Additem("Ridley");
			Combos[cdA2].Additem("Ridley");
			Combos[cArea].Additem("Tourian");
			Combos[cA1].Additem("Tourian");
			Combos[cA2].Additem("Tourian");
			Combos[cdA1].Additem("Tourian");
			Combos[cdA2].Additem("Tourian");
			Combos[cArea].Additem("Crateria");
			Combos[cA1].Additem("Crateria");
			Combos[cA2].Additem("Crateria");
			Combos[cdA1].Additem("Crateria");
			Combos[cdA2].Additem("Crateria");
			Combos[cArea].Additem("Chozodia");
			Combos[cA1].Additem("Chozodia");
			Combos[cA2].Additem("Chozodia");
			Combos[cdA1].Additem("Chozodia");
			Combos[cdA2].Additem("Chozodia");
		} else if (GameEngine->isMF())
		{
			Combos[cArea].Clear();
			Combos[cA1].Clear();
			Combos[cA2].Clear();
			Combos[cdA1].Clear();
			Combos[cdA2].Clear();

			Combos[cArea].Additem("Main Deck");
			Combos[cA1].Additem("Main Deck");
			Combos[cA2].Additem("Main Deck");
			Combos[cdA1].Additem("Main Deck");
			Combos[cdA2].Additem("Main Deck");

			Combos[cArea].Additem("SRX");
			Combos[cA1].Additem("SRX");
			Combos[cA2].Additem("SRX");
			Combos[cdA1].Additem("SRX");
			Combos[cdA2].Additem("SRX");

			Combos[cArea].Additem("TRO");
			Combos[cA1].Additem("TRO");
			Combos[cA2].Additem("TRO");
			Combos[cdA1].Additem("TRO");
			Combos[cdA2].Additem("TRO");

			Combos[cArea].Additem("PYR");
			Combos[cA1].Additem("PYR");
			Combos[cA2].Additem("PYR");

			Combos[cArea].Additem("AQA");
			Combos[cA1].Additem("AQA");
			Combos[cA2].Additem("AQA");
			Combos[cdA1].Additem("AQA");
			Combos[cdA2].Additem("AQA");
			Combos[cArea].Additem("ARC");
			Combos[cA1].Additem("ARC");
			Combos[cA2].Additem("ARC");
			Combos[cdA1].Additem("ARC");
			Combos[cdA2].Additem("ARC");

			Combos[cArea].Additem("NOC");
			Combos[cA1].Additem("NOC");
			Combos[cA2].Additem("NOC");
			Combos[cdA1].Additem("NOC");
			Combos[cdA2].Additem("NOC");

			Combos[cArea].Additem("Debug1");
			Combos[cA1].Additem("Debug1");
			Combos[cA2].Additem("Debug1");
			Combos[cdA1].Additem("Debug1");
			Combos[cdA2].Additem("Debug1");

			Combos[cArea].Additem("Debug2");
			Combos[cA1].Additem("Debug2");
			Combos[cA2].Additem("Debug2");
			Combos[cdA1].Additem("Debug2");
			Combos[cdA2].Additem("Debug2");

			Combos[cArea].Additem("Debug3");
			Combos[cA1].Additem("Debug3");
			Combos[cA2].Additem("Debug3");
			Combos[cdA1].Additem("Debug3");
			Combos[cdA2].Additem("Debug3");
		}
	} else if (combon == cClip)
	{
		if (GameEngine->isZM())
		{

			// char que
			sprintf(quick, "%s\\ZM\\clip.txt", AppPath);

		} else if (GameEngine->isMF())
		{

			sprintf(quick, "%s\\MF\\clip.txt", AppPath);

		}
		FILE *          text = fopen(quick, "rt");

		Combos[cClip].Clear();
		if (text)
		{
			while (!feof(text))
			{
				fgets(cs, 128, text);

				if ((cs[strlen(cs)] == 0xD) || (cs[strlen(cs)] == 0xA))
				cs[strlen(cs)] = 0;
				if ((cs[strlen(cs) - 1] == 0xD) || (cs[strlen(cs) - 1] == 0xA))
				cs[strlen(cs) - 1] = 0;

				Combos[cClip].Additem(cs);
			}
			fclose(text);
		}
		Combos[combon].SetListIndex(0);
		Combos[cClip].SetListIndex(0);
	}
	return 0;
}

int             ClearGlobals()
{
	int             i = 0;

	//GameEngine->isZM() = -1; // Current ROM file

	hwndTileset = NULL;

	InputHwnd = NULL;
	hTileset = NULL;
	// wcTileset=NULL;
	hwndMap = NULL;
	hMap = NULL;
	// wcMap=NULL;
	hGlobal = NULL;
	memset(&TilesetRect, 0, sizeof(TilesetRect));
	// TileCurs=(RectBuff)NULL;
	hwndResize = NULL;
	wndHE = NULL;
	for (i = 0; i < 0xC0; i++)
	memset(&MFSprSize[i], 0, sizeof(u32));
	MovingDoor[0] = 0;
	MovingDoor[1] = -1;
	MovingSprite[0] = 0;
	MovingSprite[1] = -1;
	hPal = NULL;
	SceneWnd = NULL;
	SceneGraph = NULL;
	TSScene = NULL;
	memset(&GameEngine->Members.fGbuf, 0, sizeof(GameEngine->Members.fGbuf));
	memset(&GameEngine->Members.PcPalMem, 0, sizeof(GameEngine->Members.PcPalMem));

	memset(&OverallSize, 0, sizeof(OverallSize));
	memset(&thisoam, 0, sizeof(thisoam));
	memset(&SpriteOAM, 0, sizeof(SpriteOAM));
	memset(&SprPal, 0, sizeof(SprPal));
	memset(&VRAM, 0, sizeof(VRAM));
	memset(&SpriteTable, 0, sizeof(SpriteTable));
	memset(&ZMSpritePos, 0, sizeof(ZMSpritePos));
	memset(&SpriteNo, 0, sizeof(SpriteNo));
	SceneryPic.Destroy();
	SceneryTiles.Destroy();
	for (i = 0; i < 17; i++)
	SpritePics[i].Destroy();
	memset(&DoorConnections, 0, sizeof(DoorConnections));
	for (i = 0; i < 15; i++)
	nHScroll[i] = nVScroll[i] = nMaxHScroll[i] = nMaxVScroll[i] = 0;
	for (i = 0; i < 11; i++)
	nDisplayWidth[i] = nDisplayHeight[i] = 0;
	memset(&GameEngine->Members.BGBuf, 0, sizeof(GameEngine->Members.BGBuf));
	memset(&GameEngine->Members.TSA, 0, sizeof(GameEngine->Members.TSA));
	memset(&GameEngine->Members.RoomTilesets, 0, sizeof(GameEngine->Members.RoomTilesets));
	memset(&GameEngine->Members.RoomTilesets, 0, sizeof(GameEngine->Members.RoomTilesets));

	for (i = 0; i < 5; i++)
	img[i].Destroy();
	bRunApp = 0;
	CurDoor = 0;
	RoomScrollOffset = 0;
	scrollpnt = 0;
	memset(&DoorOffsets, 0, sizeof(DoorOffsets));
	memset(&DoorOffsets, 0, sizeof(DoorOffsets));
	for (i = 0; i < 0xc; i++)
	GameEngine->Members.gScrolls[i] =  0;
	Enemy1.max = 0;
	Enemy2.max = 0;
	Enemy3.max = 0;
	/*
	* 
	* 
	* 
	* SE SpecialEffects[16]; TileBuffer TileImage; TileBuffer  BGImage;
	* TileBuffer  SpriteImage; RHeader   RoomHeader; long RoomOffsets[8];
	* long RoomOffsets[10]; nRoomBuff ThisLevelData; nRoomBuff
	* ThisBackLayer; nRoomBuff ThisForeground; nRoomBuff ThisClipData;
	* nRoomBuff ThisBackground; u8 CurMapWidth, CurMapHeight; LONG
	* oldEditProc; short Tile; char AppPath[1024]; nEnemyList Enemy1;
	* nEnemyList Enemy2; nEnemyList Enemy3; u8 LoadingLevel; HWND hDoors[40];
	* short GBASprPal[0x1000];  sDoor Doors[0xFF]; u8   DoorNum; DoorStruct
	* DoorBuf; HWND DoorWin; HWND hScroll[40];  int CurObjT; int CurObjNo; u8
	* ConnectExists; u8 PSCE[0x32];  MousePointer mpTileset;  MousePointer
	* mpMap;   int curDoor;  //Combobox Defines each for a combo int DM[3];
	* int TP[3]; int MP[3]; u8 compare; long gfxpnt_dst[16][2];  //0 = pointer 
	* to gfx, 1 = destination in VRAM long paltransfer[16][3]; //0 = pointer
	* to pal, 1 = destination, 2 = WORD count (*2 for bytes)  CompHeader
	* thiscompheader; sprite_entry sprite_in[16]; u8 maxsprite;  int
	* InputType; int SceneTile; u8 SceneType; 
	*/
	return 0;
}

int             LoadConfigFile(char *ConfigLoc)
{
	char            stringchk[256] =
	{
		0
	};
	char            valstring[10] =
	{
		0
	}; // Copies the value in the string

	char            area[] = "Default Area:";
	char            room[] = "Default Room:";
	char            fs[] = "Start Freespace:";
	int             i = 0;
	int             j = 0;
	int             k = 0;

	DefRoom = DefArea = DefAddress = 0;
	FILE *          fp = fopen(ConfigLoc, "r");

	if (fp)
	{
		fseek(fp, 0, SEEK_SET);
		while (1)
		{
			memset(&stringchk, 0, 256);
			fgets(stringchk, 128, fp);
			if (stringchk[0] == '/' && stringchk[1] == '/')
			continue;
			i = strlen(stringchk);

			if (strcmp(stringchk, "END") == 0)
			{
				return 1;
			} else if (room[strlen("Default Room")] == stringchk[strlen("Default Room")])
			{
				memcpy(&valstring, &stringchk[strlen("Default Room:")], strlen(stringchk) - strlen("Default Room:"));
				sscanf(valstring, "%d", &DefRoom);

			} else if (fs[strlen("Start Freespace")] == stringchk[strlen("Start Freespace")])
			{
				memcpy(&valstring, &stringchk[strlen("Start Freespace:")], strlen(stringchk) - strlen("Start Freespace:"));
				sscanf(valstring, "%X", &DefAddress);

			}
			k = 0;
			for (i = 0; i < strlen("Default Area"); i++)
			{
				if (stringchk[i] != area[i])
				{
					k = 0;
					break;
				}
				k++;

			}

			if (k == strlen("Default Area"))
			{
				memcpy(&valstring, &stringchk[strlen("Default Area:")], strlen(stringchk) - strlen("Default Area:"));
				sscanf(valstring, "%d", &DefArea);
			}
			j += 10;
			if (j > 50)
			return 1;

		}

		return 1;
	}
	return 0;
}
